/*
	MiniFOM4E
	IDE: IAR for ARM 6.30
*/
//---------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>

#include "stm32f10x.h"

#include "stm32f10x_conf.h"
#include "rcc.h"
#include "gpio.h"
#include "uart.h"
#include "nvic.h"
#include "tim.h"

#include "FreeRTOS.h"
#include "queue.h"
#include "task.h"
#include "timers.h"
#include "FreeRTOS_CLI.h"

uint32_t CriticalSecCntr;
xTaskHandle CLITaskHandler;

#define MDIO_DELAY 2			// Delay for MDIO/MDC functions (ms)
xTimerHandle MDIO_timer_2ms;

//---------------------------------------------------------------------------
void vTaskLED1 (void *pvParameters) {

	for (;;) {
		GPIO_SetBits(GPIOC, GPIO_Pin_11);
		vTaskDelay(500);
		GPIO_ResetBits(GPIOC, GPIO_Pin_11);
		vTaskDelay(500);
	}
}

//---------------------------------------------------------------------------
void vTimerMDIOcallback (xTimerHandle pxTimer){
	// Do something usefull =)
	GPIO_WriteBit(GPIOC, GPIO_Pin_12, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_12)));
}


//---------------------------------------------------------------------------
void main(void)
{
	// Startup periphery initialization
	RCC_Configuration();
	GPIO_Configuration();
	UartInit(UART_1);
	UartInit(UART_3);
	TIM_Configuration();
	NVIC_Configuration();

	// Background task create
	xTaskCreate (vTaskLED1,
				( signed char * ) "LED1",
				configMINIMAL_STACK_SIZE,
				NULL,
				tskLED_FLASH_PRIORITY,
				( xTaskHandle * ) NULL);
	
	xTaskCreate (vCommandLineInterfaceTask,
				( signed char * ) "CLI",
				configMINIMAL_STACK_SIZE,
				NULL,
				tskCLI_PARSE_PRIORITY,
				& CLITaskHandler);
	vTaskSuspend(CLITaskHandler);			// Suspend until UART Rx event
	
	// Timer for MDIO/MDC. Use xTimerStart(MDIO_timer_2ms, 0) to activate it.
	MDIO_timer_2ms = xTimerCreate ("MDIO/MDC delay", MDIO_DELAY, pdFALSE, (void *) 1, vTimerMDIOcallback);
				
	/* Start the scheduler. */
	vTaskStartScheduler();

    /* main while */
	while(1){}
}
//---------------------------------------------------------------------------

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

//Putchar redirection-----------------------------------------------------------
int fputc(int ch, FILE *f)
{
	// Write a character to the USART
	USART_SendData(USART1, (uint8_t) ch);

	// Loop until the end of transmission
	while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
	{}
	return ch;
}